//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// the class WebServiceConnection
    /// </summary>
    public partial class WebServiceConnection : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebServiceConnection"/> class.
        /// </summary>
        public WebServiceConnection()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public string Url
        {
            get
            {
                return this.txtURL.Text;
            }
            set
            {
                this.txtURL.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is anonymous.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is anonymous; otherwise, <c>false</c>.
        /// </value>
        public bool IsAnonymous
        {
            get
            {
                return this.rbtnAnonymous.Checked;
            }
            set
            {
                this.rbtnAnonymous.Checked = value;
                this.rbtnUserName.Checked = !value;
                
            }
        }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get
            {
                return this.ctrUserNamePassword.UserName;
            }
            set
            {
                this.ctrUserNamePassword.UserName = value;
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get
            {
                return this.ctrUserNamePassword.Password;
            }
            set
            {
                this.ctrUserNamePassword.Password = value;
            }
        }

        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsURLValid();
            if (!result.IsValid)
            {
                this.txtURL_Validating(txtURL, null);
            }

            if (!this.rbtnAnonymous.Checked)
            {
                result.Merge(this.ctrUserNamePassword.IsValid());
            }

            return result;
        }

        /// <summary>
        /// Determines whether [is server name valid].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsURLValid()
        {
            ValidationResult result = new ValidationResult(!this.txtURL.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Web Service URL is required");
            }
            else
            {
                try
                {
                    Uri uri = new Uri(this.txtURL.Text);
                }
                catch (UriFormatException ex)
                {
                    result.Messages.Add(ex.Message);
                }
            }

            return result;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbtnAnonymous control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbtnAnonymous_CheckedChanged(object sender, EventArgs e)
        {
            ctrUserNamePassword.Enabled = !rbtnAnonymous.Checked;
            if (this.rbtnAnonymous.Checked)
            {
                this.ctrUserNamePassword.ClearErrorProviders();
            }
        }

        /// <summary>
        /// Handles the OnValidation event of the ctrUserNamePassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.TridentWordAddIn.Common.ConnectionValidateEventArgs"/> instance containing the event data.</param>
        private void ctrUserNamePassword_OnValidation(object sender, ConnectionValidateEventArgs e)
        {
            this.OnValidation(sender, e);
        }

        /// <summary>
        /// Handles the Validating event of the txtURL control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtURL_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsURLValid();
                ConnectionValidateEventArgs conFormEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");
                OnValidation(txtURL, conFormEventArgs);
            }
        }
    }
}
